/**
 * @class Bible.NavigationToolbar
 * @extends Ext.Toolbar
 * @constructor Create a new NavigationToolbar
 * @param {Object} config The config object
 * @xtype navigationtoolbar
 */
(function() {

var T = Ext.Toolbar;

Bible.NavigationToolbar = Ext.extend(Ext.Toolbar, {
    /**
     * @cfg {Boolean} prependButtons
     * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
     * Defaults to <tt>false</tt>.
     */
    /**
     * @cfg {String} firstText
     * The quicktip text displayed for the first page button (defaults to <tt>'Primul Capitol'</tt>).
     * <b>Note</b>: quick tips must be initialized for the quicktip to show.
     */
    firstText : 'Primul Capitol',
    /**
     * @cfg {String} prevText
     * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
     * <b>Note</b>: quick tips must be initialized for the quicktip to show.
     */
    prevText : 'Capitolul Anterior (CTRL + Stânga)',
    /**
     * @cfg {String} nextText
     * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
     * <b>Note</b>: quick tips must be initialized for the quicktip to show.
     */
    nextText : 'Următorul Capitol (CTRL + Dreapta)',
    /**
     * @cfg {String} lastText
     * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
     * <b>Note</b>: quick tips must be initialized for the quicktip to show.
     */
    lastText : 'Ultimul Capitol',

    initComponent : function(){
        var me = this,
            pagingItems = [
                me.first = new T.Button({
                    tooltip: me.firstText,
                    overflowText: me.firstText,
                    iconCls: 'x-tbar-page-first',
                    handler: me.moveFirst,
                    scope: me
                }), me.prev = new T.Button({
                    tooltip: me.prevText,
                    overflowText: me.prevText,
                    iconCls: 'x-tbar-page-prev',
                    handler: me.movePrevious,
                    scope: me
                }),
                me.next = new T.Button({
                    tooltip: me.nextText,
                    overflowText: me.nextText,
                    iconCls: 'x-tbar-page-next',
                    handler: me.moveNext,
                    scope: me
                }), me.last = new T.Button({
                    tooltip: me.lastText,
                    overflowText: me.lastText,
                    iconCls: 'x-tbar-page-last',
                    handler: me.moveLast,
                    scope: me
                })
            ],
            userItems = me.items || me.buttons || [];
        if (me.prependButtons) {
            me.items = userItems.concat(pagingItems);
        }else{
            me.items = pagingItems.concat(userItems);
        }
        delete me.buttons;
        if(deviceType != 'mobile'){
            me.defaults = {
                scale: 'medium'
            };
        }
        Bible.NavigationToolbar.superclass.initComponent.call(me);

        me.bindStore(me.store, true);
    },

    bindStore : function(store, initial){
        var me = this;
        me.mon(store, 'load', function(s, r, o){
            var b = o.params.book,
                c = o.params.chapter,
                maxC = me.grid.booksStore.getById(b).get('chapters');
            me.first.setDisabled(b == 1 && c == 1);
            me.prev.setDisabled(b == 1 && c == 1);
            me.next.setDisabled(b == 66 && c == maxC);
            me.last.setDisabled(b == 66 && c == maxC);
        }, me);
    },
	
    // private
    getPageData : function(){
        return {
            book: this.grid.booksCombo.getValue(),
			chapter: this.grid.chaptersCombo.getValue()
        };
    },
	
    // private
    doLoad : function(params){
        this.grid.setPage(params, 1);
    },

    /**
     * Move to the first page, has the same effect as clicking the 'first' button.
     */
    moveFirst : function(){
        var o = this.getPageData();
		if (o.chapter > 1) {
			o.chapter = 1;
		} else if(o.book > 1){
			o.book--;
			o.chapter = this.grid.booksStore.getById(o.book).get('chapters');
		}
		this.doLoad(o);
    },

    /**
     * Move to the previous page, has the same effect as clicking the 'previous' button.
     */
    movePrevious : function(){
        var o = this.getPageData();
		if (o.chapter > 1) {
			o.chapter--;
		} else if(o.book > 1){
			o.book--;
			o.chapter = this.grid.booksStore.getById(o.book).get('chapters');		
		}
		this.doLoad(o);
    },

    /**
     * Move to the next page, has the same effect as clicking the 'next' button.
     */
    moveNext : function(){
        var o = this.getPageData(),
			record = this.grid.booksStore.getById(o.book);
		if (o.chapter < record.get('chapters')) {
			o.chapter++;
		} else if(o.book < 66){
			o.book++;
			o.chapter = 1;
		}
		this.doLoad(o);
    },

    /**
     * Move to the last page, has the same effect as clicking the 'last' button.
     */
    moveLast : function(){
		var o = this.getPageData(),
			record = this.grid.booksStore.getById(o.book),
            chapters = record.get('chapters');
		if (o.chapter < chapters) {
			o.chapter = chapters;
		} else if(o.book < 66){
			o.book++;
			o.chapter = 1;
		}
		this.doLoad(o);
    }
});

    Ext.reg('navigationtoolbar', Bible.NavigationToolbar);

})();
